import javax.imageio.ImageIO;
import javax.swing.*;

import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;

public class Wenjian {
String wname  = MouseClickExample.name;
    static JFrame jFrame = new JFrame();
    int x = MouseClickExample.screenCapture.getWidth();
    int y = MouseClickExample.screenCapture.getHeight();
    BufferedImage bufferedImage;
    FileInputStream fileInputStream;
    JMenuBar menuBar;
    JMenuItem m1;
    BufferedImage image;

    public void wen(){
//        jFrame.setUndecorated(true);
        jFrame.setLayout(null);
        jFrame.setTitle(wname);
        jFrame.setBounds(100,100,1500,1000);
        try {
            fileInputStream = new FileInputStream(wname);
            bufferedImage = ImageIO.read(fileInputStream);
        }  catch (IOException e) {
            e.printStackTrace();
        }
        Font f = new Font("隶书",Font.PLAIN,20);
         menuBar = new JMenuBar();
        JMenu m2 = new JMenu("文件");
//        = new JMenuItem("另存为");

        try {
            FileInputStream file = new FileInputStream(wname);
            image = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JMenuItem m1 = new JMenuItem(new AbstractAction("另存为") {
            @Override
            public void actionPerformed(ActionEvent e) {
//                FileSystemView fsv = FileSystemView.getFileSystemView();//获取桌面路径
//                JFileChooser fileChooser = new JFileChooser(fsv.getHomeDirectory());//写入"."是打开当前项目路径
//                int result = fileChooser.showOpenDialog(null);
//                if(result == JFileChooser.APPROVE_OPTION) {
//                System.out.println("path: " + fileChooser.getSelectedFile().getAbsolutePath());
//                    fileChooser.getSelectedFile().getAbsolutePath();
//                }
                FileSystemView fsv = FileSystemView.getFileSystemView();
                JFileChooser fileChooser = new JFileChooser(fsv.getHomeDirectory());
                fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                fileChooser.setSelectedFile(new File(wname) );
                int result = fileChooser.showOpenDialog(null);
                System.out.println(result);
                if(result == JFileChooser.APPROVE_OPTION) {
                    fileChooser.getSelectedFile().getAbsolutePath();
                System.out.println("path: " + fileChooser.getSelectedFile().getAbsolutePath());
 }
                File file = fileChooser.getSelectedFile();
                try {
                    ImageIO.write(image, "jpg", file);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });
        m1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String currentPath = System.getProperty("user.dir");
                System.out.println("当前路径：" + currentPath);

            }
        });
        JMenuItem open = new JMenuItem("打开");
        JMenuItem close = new JMenuItem("关闭");
        menuBar.add(m2);
        menuBar.add(m1);
        m1.setFont(f);
        m2.setFont(f);
        open.setFont(f);
        close.setFont(f);
        m2.add(open);
        m2.addSeparator();//将分隔条加入到菜单中
        m2.add(close);
        jFrame.setJMenuBar(menuBar);

        ImageIcon imageIcon = new ImageIcon(bufferedImage);

        JLabel label = new JLabel(imageIcon);
        label.setBounds(0,0 , 1500, 1200);
        jFrame.add(label);

        label.setHorizontalAlignment(SwingUtilities.CENTER);
        jFrame.setVisible(true);
    }
    public void xwen(){
    }
//    public static void main(String[] args) {
// new Wenjian().wen();
//    }
}
